package com.bifang.module.file.config.set;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * 线程池配置类
 */
@Configuration
public class FileExecutorConfig {
    @Bean(name = "commonTaskExecutor")
    public ThreadPoolTaskExecutor commonTaskExecutor() {
        ThreadPoolTaskExecutor poolTaskExecutor = new ThreadPoolTaskExecutor();
        // 线程池维护线程的最少数量
        poolTaskExecutor.setCorePoolSize(1);
        // 线程池维护线程的最大数量
        poolTaskExecutor.setMaxPoolSize(20);
        // 设置默认线程名称
        poolTaskExecutor.setThreadNamePrefix("commmonLog-");
        // 线程池对拒绝任务(无线程可用)的处理策略 ThreadPoolExecutor.CallerRunsPolicy策略 ,调用者的线程会执行该任务,如果执行器已关闭,则丢弃.
        poolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 线程池所使用的缓冲队列
        poolTaskExecutor.setQueueCapacity(20);
        // 线程池维护线程所允许的空闲时间 单位秒
        poolTaskExecutor.setKeepAliveSeconds(20);
        // 等待所有任务结束后再关闭线程池
        poolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);
        poolTaskExecutor.initialize();
        return poolTaskExecutor;
    }
}
